var current_json = '';
var current_json_str = '';

$(function () {
    $('.tab').pignoseTab({
        animation: true,
        children: '.tab'
    });
});

$('#json-src').keyup(function () {
    var content = $.trim($('#json-src').val());
    var result = '';
    if (content != '') {
        try {
            current_json = jsonlint.parse(content);
            current_json_str = JSON.stringify(current_json);
            result = new JSONFormat(content, 4).toString();
        } catch (e) {
            result = '<span style="color: #f1592a;font-weight:bold;">' + e + '</span>';
            current_json_str = result;
        }
        $('#json-target').html(result);
    } else {
        $('#json-target').html('');
    }
})
new Clipboard('.copy');
new Clipboard('.copy2');

$('#btn-yszy').click(function () {
    var v = $('#json-str').val();
    n = yasuo2(3, v).replace(/[\r\n]/g, "");
    $('#json-str').val(n);
})

$('#btn-ys').click(function () {
    yasuo(1)
})
$('#btn-zy').click(function () {
    yasuo(2)
})
$('#btn-qczy').click(function () {
    yasuo(4)
})
$('#btn-qk').click(function () {
    $('#json-str').val('');
})

function yasuo2(ii, text) {
    if (ii == 1 || ii == 3) {
        text = text.split("\n").join(" ");
        var t = [];
        var inString = false;
        for (var i = 0, len = text.length; i < len; i++) {
            var c = text.charAt(i);
            if (inString && c === inString) {
                // TODO: \\"
                if (text.charAt(i - 1) !== '\\') {
                    inString = false;
                }
            } else if (!inString && (c === '"' || c === "'")) {
                inString = c;
            } else if (!inString && (c === ' ' || c === "\t")) {
                c = '';
            }
            t.push(c);
        }
        text = t.join('');
    }
    if (ii == 2 || ii == 3) {
        text = text.replace(/\\/g, "\\\\").replace(/\"/g, "\\\"");
    }
    if (ii == 4) {
        text = text.replace(/\\\\/g, "\\").replace(/\\\"/g, '\"');
    }
    return text;
}

function yasuo(e) {
    var t = $('#json-str').val();
    if (t = t.replace(/[\r\n]/g, ""),
    1 == e || 3 == e) {
        for (var n = [], o = !1, i = 0, r = t.length; r > i; i++) {
            var a = t.charAt(i);
            o && a === o ? "\\" !== t.charAt(i - 1) && (o = !1) : o || '"' !== a && "'" !== a ? o || " " !== a && "	" !== a || (a = "") : o = a,
                n.push(a)
        }
        t = n.join("")
    }
    (2 == e || 3 == e) && (t = t.replace(/\\/g, "\\\\").replace(/\"/g, '\\"')),
    4 == e && (t = t.replace(/\\\\/g, "\\").replace(/\\\"/g, '"')),
        $('#json-str').val(t);
}
